package com.cyl.manager.lbs.controller;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.cyl.manager.lbs.convert.LbsZbjlConvert;
import com.cyl.manager.lbs.domain.LbsProduct;
import com.cyl.manager.lbs.domain.LbsZbjl;
import com.cyl.manager.lbs.pojo.query.LbsZbjlQuery;
import com.cyl.manager.lbs.service.LbsZbjlService;
import com.cyl.manager.lbs.pojo.vo.LbsZbjlVO;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 直播列表Controller
 *
 * @author zcc
 * @date 2025-02-09
 */
@Api(description ="直播列表接口列表")
@RestController
@RequestMapping("/mall/lbsZbjl")
public class LbsZbjlController extends BaseController {
    @Autowired
    private LbsZbjlService service;
    @Autowired
    private LbsZbjlConvert convert;

    @ApiOperation("查询直播列表列表")
    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:list')")
    @PostMapping("/list")
    public ResponseEntity<Page<LbsZbjl>> list(@RequestBody LbsZbjlQuery query, Pageable page) {
    	Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
        	query.setDeptZbjId(qrDeptZbjId);
        }
        List<LbsZbjl> list = service.selectList(query, page);
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }

    @ApiOperation("导出直播列表列表")
    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:export')")
    @Log(title = "直播列表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<String> export(LbsZbjlQuery query) {
    	Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
        	query.setDeptZbjId(qrDeptZbjId);
        }
        List<LbsZbjl> list = service.selectList(query, null);
        ExcelUtil<LbsZbjlVO> util = new ExcelUtil<>(LbsZbjlVO.class);
        return ResponseEntity.ok(util.writeExcel(convert.dos2vos(list), "直播列表数据"));
    }

    @ApiOperation("获取直播列表详细信息")
    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<LbsZbjl> getInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(service.selectByPushInfoAPIId(id));
    }

    
    @ApiOperation("获取直播列表详细信息")
    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:query')")
    @GetMapping(value = "/getPushInfo/{id}")
    public ResponseEntity<LbsZbjl> getPushInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(service.selectByPushInfoAPIId(id));
    }
    
    
    
    @ApiOperation("新增直播列表")
    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:add')")
    @Log(title = "直播列表", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody LbsZbjl lbsZbjl) {
    	lbsZbjl.setZbjzt("102");
        return ResponseEntity.ok(service.insert(lbsZbjl));
    }

    @ApiOperation("修改直播列表")
    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:edit')")
    @Log(title = "直播列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody LbsZbjl lbsZbjl) {
    	 // return ResponseEntity.ok(service.activityProductEnable(lbsZbjl));
    	LbsZbjl lbsZbjldb = service.selectById(lbsZbjl.getId());
    	if(!"102".equals(lbsZbjldb.getZbjzt())) {
	        throw new RuntimeException("未开始状态才能修改");
    	}
       return ResponseEntity.ok(service.update(lbsZbjl));
    }
    
    @ApiOperation("是否启用商品卡片")
    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:edit')")
    @Log(title = "是否启用商品卡片", businessType = BusinessType.UPDATE)
    @PutMapping("/activityProductEnable")
    public ResponseEntity<Integer> activityProductEnable(@RequestBody LbsZbjl lbsZbjl) {
        return ResponseEntity.ok(service.activityProductEnable(lbsZbjl));
    }
    

    @ApiOperation("删除直播列表")
    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:remove')")
    @Log(title = "直播列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
    	LbsZbjl lbsZbjldb = service.selectById(ids[0]);
    	if(!"102".equals(lbsZbjldb.getZbjzt())) {
	        throw new RuntimeException("未开始状态才能删除");
    	}
        return ResponseEntity.ok(service.deleteByIds(ids));
    }
    
    
}
